#include "optionswidget.h"
#include "filetable.h"
#include "slepui.h"

#include "slepbase.h"
#include "cslep.h"



OptionsWidget::OptionsWidget(SLEP_METHOD method){
	this->method = method;
    QVBoxLayout *layout = new QVBoxLayout;
    tabWidget = new QTabWidget;
    opts = (OPTS*) malloc(sizeof(OPTS));
    memset(opts,0,sizeof(opts));
	A=0;
	y=0;

	itemC0Vector = 0;
	itemX0 = 0;
	itemx0 = 0;

	if(method == Method_Least_R || method == Method_nnLeast_R || method == Method_Least_C || method == Method_nnLeast_C
		|| method == Method_Logistic_R || method == Method_nnLogistic_R || method == Method_Logistic_C || method == Method_nnLogistic_C
		|| method == Method_glLeast_R || method == Method_glLogistic_R){
			opts->c0 = new slep_vector[1];
			opts->c0->dim = 1;
			opts->c0->sparsity = SlepDense;
			opts->c0->inc = 1;
			opts->c0->data.vctr = new double[1];
	}else if( method == Method_mtLeast_R || method == Method_mtLeast_C 
		|| method == Method_mcLeast_R || method == Method_mcLeast_C){
			opts->c0 = new slep_vector[1];
			opts->c0->dim = 1;
			opts->c0->sparsity = SlepDense;
			opts->c0->inc = 1;
			opts->c0->data.vctr = new double[1];
	}

    setLayout(layout);
    layout->addWidget(tabWidget);
    createStartOptions();
    createTerminationOptions();
    createNormalizationOptions();
    createReguarizationParameterOptions();
    createMethodFlags();
    createInputTab();
    createButtons();
    createActions();
	switch(method){
		case Method_Least_R:
			setWindowTitle(tr("Least R"));
			break;
		case Method_Least_C:
			setWindowTitle(tr("Least C"));
			break;
		case Method_glLogistic_R:
			setWindowTitle(tr("Options(Group Lasso w/ Logistic Loss)"));
			break;
	}

}


void OptionsWidget::createButtons(){
    QLayout *layout = this->layout();
    QHBoxLayout *hLayout = new QHBoxLayout;
    QWidget *buttonBox = new QWidget;
    buttonOk = new QPushButton(tr("Run"),this);
    buttonCancel = new QPushButton(tr("Cancel"),this);
	((QBoxLayout*)hLayout)->addStretch();
    hLayout->addWidget(buttonOk);
    hLayout->addWidget(buttonCancel);
    buttonBox->setLayout(hLayout);
    layout->addWidget(buttonBox);
}



void OptionsWidget::createActions(){
    connect(spinBoxMaxIter,SIGNAL(valueChanged(int)),this, SLOT(setOPTS_maxIter(int)));
    connect(dspinBoxTolerance,SIGNAL(valueChanged(double)),this,SLOT(setOPTS_tol(double)));
    connect(buttonGroupTFlag,SIGNAL(buttonClicked(int)),this,SLOT(setOPTS_tFlag(int)));

    connect(buttonGroupInit,SIGNAL(buttonClicked(int)),this,SLOT(setOPTS_init(int)));
    connect(buttonGroupNorm,SIGNAL(buttonClicked(int)),this,SLOT(setOPTS_nFlag(int)));

    connect(buttonGroupMFlag,SIGNAL(buttonClicked(int)),this,SLOT(setOPTS_mFlag(int)));
    connect(buttonGroupLFlag,SIGNAL(buttonClicked(int)),this,SLOT(setOPTS_lFlag(int)));

    connect(buttonGroupRFlag, SIGNAL(buttonClicked(int)),this,SLOT(setOPTS_rFlag(int)));
    connect(spinBoxRsL2,SIGNAL(valueChanged(double)),this, SLOT(setOPTS_rsL2(double)));

    connect(buttonOk,SIGNAL(clicked()),this,SLOT(prepareData()));
    connect(buttonCancel,SIGNAL(clicked()),this,SLOT(dontRun()));
}



OPTS* OptionsWidget::getOPTS(){
	return opts;
}


slep_matrix* OptionsWidget::getInputA(){
	return A;
}


slep_vector* OptionsWidget::getInputy(){
	return y;
}

double OptionsWidget::getL1RegCoefficient(){
	return spinBoxRsL1->value();
}


